package com.demo.study.thread;


import org.junit.jupiter.api.Test;

public class T04_WaitNotify {
    private static final Object LOCK = new Object();

    @Test
    public void test3() throws InterruptedException {
        Thread thread1 = new Thread(() -> {
           synchronized (LOCK) {
               System.out.println("thread1 执行了");
               try {
                   Thread.sleep(3000);
                   System.out.println("thread1 醒来了了");
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        });

        Thread thread2 = new Thread(() -> {
            synchronized (LOCK) {
                System.out.println("thread2 执行了");
                try {
                    LOCK.wait();
                    System.out.println("thread2 醒来了了");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        Thread thread3 = new Thread(() -> {
            synchronized (LOCK) {
                System.out.println("thread3 执行了");
                try {
                    LOCK.wait();
                    System.out.println("thread3 醒来了了");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
        Thread.sleep(10000);
    }

    @Test
    public void test1() {
        Thread t1 = new Thread(() -> {
            synchronized (LOCK) {
                try {
                    System.out.println("t1...");
                    test2();
                    //                    LOCK.wait();
                    Thread.sleep(3000);
                    System.out.println("t1...wait...");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (LOCK) {
                try {
                    System.out.println("t2...");
                    Thread.sleep(3000);
                    System.out.println("t2...wait...");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        t1.start();
        t2.start();
    }

    private static void test2() {
        synchronized (LOCK) {
            System.out.println("test2...");
        }
    }
}
